#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    fTimer = new QTimer(this);
    fTimer->stop();
    fTimer->setInterval(1000);

    connect(fTimer,SIGNAL(timeout()),this,SLOT(on_timer_timeout()));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_btnGetCurDateTime_clicked()
{
    QDateTime curDateTime = QDateTime::currentDateTime();

    ui->timeEdit->setTime(curDateTime.time());
    ui->editTime->setText(curDateTime.toString("hh:mm:ss"));

    ui->dateEdit->setDate(curDateTime.date());
    ui->editDate->setText(curDateTime.toString("yyyy-MM-dd"));

    ui->dateTimeEdit->setDateTime(curDateTime);
    ui->editDateTime->setText(curDateTime.toString("yyyy-MM-dd hh:mm:ss"));
}


void Widget::on_btnSetTime_clicked()
{
    QString str = ui->editTime->text();
    str = str.trimmed();
    if(!str.isEmpty())
    {
        QTime time_in = QTime::fromString(str,"hh:mm:ss");
        ui->timeEdit->setTime(time_in);
    }
}

void Widget::on_btnSetDate_clicked()
{
    QString str = ui->editDate->text();
    str = str.trimmed();
    if(!str.isEmpty())
    {
        QDate date_in = QDate::fromString(str,"yyyy-MM-dd");
        ui->dateEdit->setDate(date_in);
    }
}

void Widget::on_btnSetDateTime_clicked()
{
    QString str = ui->editDateTime->text();
    str = str.trimmed();
    if(!str.isEmpty())
    {
        QDateTime date_time_in = QDateTime::fromString(str,"yyyy-MM-dd hh:mm:ss");
        ui->dateTimeEdit->setDateTime(date_time_in);
    }
}

void Widget::on_btnStart_clicked()
{
    fTimer->start();
    fTimeCounter.start();
    ui->btnStart->setEnabled(false);
    ui->btnStop->setEnabled(true);
    ui->btnSetIntv->setEnabled(false);

}

void Widget::on_calendarWidget_selectionChanged()
{
    QDate dt = ui->calendarWidget->selectedDate();
    QString str = dt.toString(tr("yyyy-MM-dd"));
    ui->editCheckDateTime->setText(str);
}

void Widget::on_timer_timeout()
{
    QTime curTime =QTime::currentTime();
    ui->lcdHour->display(curTime.hour());
    ui->lcdMin->display(curTime.minute());
    ui->lcdSec->display(curTime.second());

    int va = ui->progressBar->value();
    va++;
    if(va>100)
    {
        va = 0;
    }
    ui->progressBar->setValue(va);

}

void Widget::on_btnStop_clicked()
{
    fTimer->stop();
    int tmMsec = fTimeCounter.elapsed();
    int ms = tmMsec%1000;
    int sec = tmMsec/1000;
    QString str = QString::asprintf("流逝时间： %ds  %dms ", sec, ms);
    ui->LabElapsTime->setText(str);
    ui->btnStart->setEnabled(true);
    ui->btnStop->setEnabled(false);
    ui->btnSetIntv->setEnabled(true);
}
